﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_CertifiedTamNgung_Detail
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") And Not Request.QueryString("CertifiedID").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                hidCertifiedId.Value = Request.QueryString("CertifiedID")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As CertifiedTamNgung = (From q In data.CertifiedTamNgungs Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                'filter theo curHuyenId And q.ProvinceId = curHuyenId
                Dim j = (From k In data.CertifiedHouseholds Where k.CertifiedID = p.CertifiedID _
                                           Order By k.CertifiedUp_Date Descending Select k).FirstOrDefault
                If Not j Is Nothing Then
                    hidCertifiedId.Value = IIf(IsNothing(j.CertifiedID) = True, "", j.CertifiedID)
                    hidCertifiedCode.Value = IIf(IsNothing(j.CertifiedCode) = True, "", j.CertifiedCode)
                    'Tên người đăng ký kinh doanh
                    txtBiz_VietNam.Text = IIf(IsNothing(j.Biz_VietName) = True, "", j.Biz_VietName)
                    'Mã cấp giấy chứng nhận
                    txtCertifiedCode.Text = IIf(IsNothing(j.CertifiedCode) = True, "", j.CertifiedCode)
                    'Tên văn phòng cấp giấy chứng nhận
                    txtCertifiedDeptName.Text = IIf(IsNothing(j.CertifiedDeptName) = True, "", j.CertifiedDeptName)
                    'Ngày cấp giấy chứng nhận
                    If Not j.CertifiedDate Is Nothing Then
                        txtCertifiedDate.Text = CType(j.CertifiedDate, Date).ToString("dd/MM/yyyy")
                    End If
                    'địa điểm kinh doanh
                    txtBiz_HeadOffice.Text = IIf(IsNothing(j.Biz_VietName) = True, "", j.Biz_VietName)
                    'điên thoại
                    txtOw_Tel.Text = IIf(IsNothing(j.Ow_Tel) = True, "", j.Ow_Tel)
                    'fax
                    txtOw_Fax.Text = IIf(IsNothing(j.Ow_Fax) = True, "", j.Ow_Fax)
                    'Email
                    txtOw_Email.Text = IIf(IsNothing(j.Ow_Email) = True, "", j.Ow_Email)
                    'website
                    txtOw_WebSite.Text = IIf(IsNothing(j.Ow_WebSite) = True, "", j.Ow_WebSite)
                End If
                txtThoigiantamngung.Text = IIf(IsNothing(p.ThoiGianTamNgung) = True, "", p.ThoiGianTamNgung)
                If Not p.BatDate Is Nothing Then
                    txtBatdate.Text = CType(p.BatDate, Date).ToString("dd/MM/yyyy")
                End If
                If Not p.KetThuc Is Nothing Then
                    txtKetthuc.Text = CType(p.KetThuc, Date).ToString("dd/MM/yyyy")
                End If
                txtLydo.Text = IIf(IsNothing(p.LyDo) = True, "", p.LyDo)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As CertifiedTamNgung = (From q In data.CertifiedTamNgungs Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.CertifiedID = hidCertifiedId.Value
                p.CertifiedCode = hidCertifiedCode.Value
                p.ThoiGianTamNgung = txtThoigiantamngung.Text.Trim()
                p.BatDate = StringToDate(txtBatdate.Text.Trim())
                p.KetThuc = StringToDate(txtKetthuc.Text.Trim())
                p.LyDo = txtLydo.Text.Trim()
                p.StatusId = TinhTrangHoSo.Nhap
                p.NguoiSua = Session("UserName")
                p.NgaySua = Now
                p.HuyenId = Session("HuyenId")
                data.SaveChanges()
                Dim strLog As String = "Giay CN:" & txtCertifiedCode.Text.Trim() & ", tên hộ: " & txtBiz_VietNam.Text.Trim()
                Insert_App_Log("Edit  Tam ngừng kinh doanh:" & strLog & "", Function_Name.CertifiedTamNgung, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/CertifiedTamNgung/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
